﻿
@{
    Layout = null;
}
@model AdminModel

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="~/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="~/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="~/css/site.css" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">@(Model.AdminId>0?"修改信息":"添加管理员")</div>
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form" lay-filter="form-group">
                    <div class="layui-form-item">
                        <label class="layui-form-label">登录名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="UserName" lay-verify="usernmae" autocomplete="off" placeholder="请输入登录名称" class="layui-input w300" @Html.Raw(Model.AdminId > 0 ? "disabled=\"disabled\"" : "") value="@Model.UserName">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">@(Model.AdminId == 0 ? "初始密码" : "修改密码")</label>
                        <div class="layui-input-inline">
                            <input type="password" name="Password" lay-verify="pass" placeholder="@(Model.AdminId == 0 ? "初始密码" : "不填写则不修改")" autocomplete="off" class="layui-input w300">
                        </div>
                        <div class="layui-form-mid layui-word-aux" style="margin-left:110px;">请填写6到12位密码</div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="RealName" lay-verify="required" placeholder="请输入用户姓名" autocomplete="off" class="layui-input w300" value="@Model.RealName">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="chkIsActive" lay-skin="switch" @Html.Raw(Model.IsActive == true ? " checked=\"checked\"" : "") lay-text="启用|禁用">
                        </div>
                    </div>                  
                    <div class="layui-form-item" lay-filter="component-form-element">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <div class="layui-col-md12">
                                @foreach (var item in Model.RoleList)
                                {
                                    <input type="checkbox" name="chkRoleId" value="@item.RoleId" title="@item.RoleName" lay-skin="primary" @Html.Raw(Model.Roles.Contains(item.RoleId.ToString()) == true ? "checked=\"checked\"" : "")>
                                }
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="Remark" placeholder="请输入内容" class="layui-textarea w300">@Model.Remark</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0;">
                                <button class="layui-btn" lay-submit="" lay-filter="form-page">立即保存</button>
                                <a href="/Manager/Index" class="layui-btn layui-btn-primary">返回列表</a>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="Roles" id="Roles" />
                </form>
            </div>
        </div>
    </div>
    <script src="~/layuiadmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form'], function () {
            var $ = layui.$
                , admin = layui.admin
                , element = layui.element
                , layer = layui.layer
                , form = layui.form;

            form.render(null, 'form-group');

            /* 自定义验证规则 */
            form.verify({
                usernmae: function (value) {
                    if (value.length < 4) {
                        return '登录名称不能少于4个字符';
                    }
                }
                , pass: function (value) {
                    if (@Html.Raw(Model.AdminId > 0 ? "value.length > 0 &&" : "") value.length < 6) {
                        return '密码必须6到12位';
                    }
                }
            });

            ///* 监听提交 */
            form.on('submit(form-page)', function (data) {
                var rolesArr = [];
                $('input[name=chkRoleId]:checked').each(function() {
                    rolesArr.push($(this).val());
                });
                data.field.IsActive = data.field.chkIsActive == 'on' ? true : false;
                data.field.Roles = rolesArr;// rolesArr.join(",");
                console.log(data.field);

                $.ajax({                  
                    type: "post",
                    url: '/manager/edit/@Model.AdminId',
                    data: data.field,
                    //async: false,
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1000) {
                            layer.alert(res.msg, function (index) {
                                window.location.href = '/manager/index';//do something
                                layer.close(index);
                            });
                        }
                        else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
                //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                return false;
            });
        });
    </script>
</body>
</html>

